/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package imageuploader.ui;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.*;
import javax.imageio.*;


/**
 *
 * @author nir
 */
public class ImagesPanel extends JPanel{

    GridLayout layout;
    JLabel message;
    Canvas[] canvases;
    int tmb_width, tmb_height;
    ArrayList<File> chosenFiles;
    
    
    public ImagesPanel(){
        
        tmb_height = 90;
        tmb_width = 120;
        chosenFiles = new ArrayList<File>();
        message = new JLabel("Choose a directory and then upload pictures!");
        add(message);
        
    }
    
    public File[] getChosenFiles(){
        File[] files = new File[chosenFiles.size()];
        for(int i=0;i<files.length;i++){
            files[i] = chosenFiles.get(i);
        }
        return files;
    }
    
    public Dimension getThumbSize(){
        return new Dimension(tmb_width+2, tmb_height+2);
    }
    
    public void setColumns(int cols){
        if(layout instanceof GridLayout){
            layout = new GridLayout(0, cols, 1, 1);
            setLayout(layout);
        }
    }
    
    public int getColumns(){
        return layout==null?0:layout.getColumns();
    }
    
    public void showImages(File folder) throws IOException{
        removeAll();
        File[] files = folder.listFiles(new ImageFileFilter());
        System.out.println(files.length);
        if(files.length>0){
            // Change layout
            layout = new GridLayout(0, 4, 1, 1);
            setLayout(layout); 
            for(File file: files){
                add(createButton(file));
            }
        }else{
            // Show a message
            message = new JLabel("There isn't images in this folder");
            add(message);
        }
        validate();
        repaint();
    }
    
    public JButton createButton(File file) throws IOException{
        JButton btn = new JButton(createThunmbnail(file));
        btn.setBorderPainted(false);
        btn.setBackground(null);
        btn.setMargin(new Insets(1,1,1,1));
        //btn.setSize(new Dimension(tmb_width, tmb_height));
        btn.addActionListener(new ImageClickListener(file));
        return btn;
    }
    
    public ImageIcon createThunmbnail(File file) throws IOException{
        BufferedImage image, scaled;
        Graphics2D g;
        
        image = ImageIO.read(file);
        scaled = new BufferedImage(tmb_width, tmb_height, BufferedImage.TYPE_INT_RGB);
        g = scaled.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                            RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g.drawImage(image, 0, 0, tmb_width, tmb_height, null);
        
        return new ImageIcon(scaled);
    }
    
    public static void main(String[] args){
    }
    
    private class ImageClickListener implements ActionListener{

        boolean isAlreadyClicked = false;
        File handledFile;

        public ImageClickListener(File file){
            handledFile  = file;
        }

        public void actionPerformed(ActionEvent event) {
            if(isAlreadyClicked){
                isAlreadyClicked = false;
                chosenFiles.remove(handledFile);
                ((JButton)event.getSource()).setBackground(null);
            }else{
                isAlreadyClicked = true;
                chosenFiles.add(handledFile);
                ((JButton)event.getSource()).setBackground(Color.BLUE);
            }
        }
    
    }
    
    private class ImageFileFilter implements FileFilter{

        String[] accepted;


        public ImageFileFilter(){
            accepted = new String[]{".gif", ".jpg", ".jpeg", ".png" , ".bmp" , ".swf"};
        }

        public boolean accept(File file) {
            String name = file.getName();
            for(int i=0;i<accepted.length;i++){
                if(name.endsWith(accepted[i]) || name.endsWith(accepted[i].toUpperCase())) return true;
            }
            return false;
        }

    }
    
    public void resizeColumns(int frameWidth, int scrollBarWidth, int tmbWidth, int nColumns){

            int currentSize = frameWidth - scrollBarWidth;

            int currentNetoSize = tmbWidth*nColumns,
                diff = currentSize - currentNetoSize - 37,
                nTmb = diff/tmbWidth;                  


            if(currentSize<currentNetoSize && (nColumns-1) > 0){
                setColumns(nColumns-1);
                return;
            }

            if( (Math.abs(diff)==tmbWidth) || (nTmb!=0)){
                if(nColumns+nTmb > 0){
                    setColumns(nColumns+nTmb);
                }
            }        
    }
    
}



